package pactera.tf.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import pactera.tf.dataobj.ChatFriendsDO;
import pactera.tf.dataobj.UserinfoDO;
import pactera.tf.mapper.ChatFriendsMapper;
import pactera.tf.service.ChatFriendsService;
import pactera.tf.service.RedisService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChatFriendsServiceImpl implements ChatFriendsService {
    @Autowired
    ChatFriendsMapper chatFriendsMapper;
    @Resource
    private StringRedisTemplate strtemp;
    @Autowired
    private RedisService redisService;


    @Override
    public List<ChatFriendsDO> lookUserAllFriends(String userid) {
        List <ChatFriendsDO> list = chatFriendsMapper.lookUserAllFriends(userid);
        List <ChatFriendsDO> lists = new ArrayList<ChatFriendsDO>();
        for (ChatFriendsDO chatFriendsDO : list) {
            String state = redisService.get("onlin-user"+chatFriendsDO.getUserid());
            if("Y".equals(state)) {
                chatFriendsDO.setState("在线");
            }else {
                chatFriendsDO.setState("离线");
            }
            lists.add(chatFriendsDO);
        }
        return lists;
    }

    @Override
    public void insertUserFriend(ChatFriendsDO chatFriends) {
        chatFriendsMapper.insertUserFriend(chatFriends);

    }

    @Override
    public Integer justTwoUserIsFriend(ChatFriendsDO chatFriends) {
        return chatFriendsMapper.justTwoUserIsFriend(chatFriends);
    }

    @Override
    public UserinfoDO lkUserinfoByUserid(String userid) {
        return chatFriendsMapper.lkUserinfoByUserid(userid);
    }

    @Override
    public void updateuselive(UserinfoDO userinfoDO) {
        chatFriendsMapper.updateuselive(userinfoDO);
    }
}
